//
//  DataModel.swift
//  TSKit
//
//  Created by chenyusen on 2018/2/6.
//  Copyright © 2018年 TechSen. All rights reserved.
//

import UIKit

public protocol DataModelCompatible: class {
    
    
    /// 数据是否已经加载完成
    var isLoaded: Bool { get }
    
    
    /// 数据是否正在加载
    var isLoading: Bool { get }
    
    
    /// 数据是否已经过期
    var isOutdated: Bool { get }
    
    
    var isLoadingMore: Bool { get }
    /// 加载数据
    ///
    /// - Parameter more: 是否是加载更多, 用于分页页面
    func load(more: Bool)
    
    
    /// 取消数据加载
    func cancel()
    
    
    /// 监听当前DataModel的代理对象
    var delegates: NSHashTable<AnyObject> { get set }
    
    
}

extension DataModelCompatible {
    
    public var isLoaded: Bool {
        return true
    }
    
    public var isLoading: Bool {
        return false
    }
    
    public var isLoadingMore: Bool {
        return false
    }
    
    public var isOutdated: Bool {
        return false
    }
    
    public func load(more: Bool) {}
    
    public func cancel() {}
    
    /// 通知所有订阅者数据已经开始加载
    public func didStartLoad() {
        delegates.allObjects.forEach { ($0 as? DataModelDelegateCompatible)?.dataModel(didStartLoad: self) }
    }
    
    /// 通知所有订阅者数据已经加载完毕
    ///
    /// - Parameter data: 加载完毕的数据
    public func didFinishLoad(data: Any?) {
        delegates.allObjects.forEach { ($0 as? DataModelDelegateCompatible)?.dataModel(self, didFinishLoad: data) }
    }
    
    /// 通知所有订阅者,数据加载失败
    ///
    /// - Parameter error: 错误信息
    public func didFaildLoad(error: Error) {
        delegates.allObjects.forEach { ($0 as? DataModelDelegateCompatible)?.dataModel(self, didFailLoad: error) }
    }
    
    /// 通知所有订阅者,已经取消加载数据
    public func didCancelLoad() {
        delegates.allObjects.forEach { ($0 as? DataModelDelegateCompatible)?.dataModel(didCancelLoad: self) }
    }
    
    
    /// 通知所有订阅者,数据模型已经改变
    public func didChange() {
        delegates.allObjects.forEach{ ($0 as? DataModelDelegateCompatible)?.dataModel(didChange: self) }
    }
}

public class DefaultDataModel: NSObject, DataModelCompatible {
    public var delegates: NSHashTable<AnyObject> = NSHashTable<AnyObject>()
    
    
}

public protocol DataModelDelegateCompatible: class {
    
    
    /// 数据开始加载
    ///
    /// - Parameter model: DataModelCompatible实例
    func dataModel(didStartLoad model: DataModelCompatible)
    
    
    /// 数据加载完毕
    ///
    /// - Parameters:
    ///   - model: 数据加载完毕
    ///   - data: 加载完毕获取的数据
    func dataModel(_ model: DataModelCompatible, didFinishLoad data: Any?)
    
    
    /// 数据加载失败
    ///
    /// - Parameters:
    ///   - model: DataModelCompatible实例
    ///   - error: 错误信息
    func dataModel(_ model: DataModelCompatible, didFailLoad error: Error)
    
    
    /// 数据加载被取消
    ///
    /// - Parameter model: DataModelCompatible实例
    func dataModel(didCancelLoad model: DataModelCompatible)
    
    
    /// 数据模型被改变
    ///
    /// - Parameter model: DataModelCompatible实例
    func dataModel(didChange model:DataModelCompatible)
}
